(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.currency.createContinueController', createContinueController);
	createContinueController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'currencyService' ];
	function createContinueController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, currencyService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.currency = {};
		
		ctrl.init = function() {			
			authService.createContinue(params).then(function(response) {
				ctrl.currency = response.data.model;
				ctrl.currency.taskId = response.data.task.taskId;
				currencyService.getCountryOptions().then(function(response) {
					ctrl.countryCodeOptions = response;
				}, function(error) {
					log.debug('can\'t load country code options' + angular.toJson(error));
				});
				currencyService.getIntCalcBasisOptions().then(function(response) {
					ctrl.calbaseOptions = response;
				}, function(error) {
					log.debug('can\'t load interest calc options' + angular.toJson(error));
				});		
			});
			ctrl.decimalNumOption = getDecimalNumOption();
			
		};
		function getDecimalNumOption() {
			var option = {
					"data" : [
					 {
						"value" : 1,
						"label" : 1
					}, {
						"value" : 2,
						"label" : 2
					}, {
						"value" : 3,
						"label" : 3
					} ]
			};
			return option;
		}

		ctrl.createContinueSave = function() {
			return authService.createContinueSave(ctrl.currency).then(saveSuccessful, saveFailed);
		};

		ctrl.createContinueSubmit = function() {
			return authService.createContinueSubmit(ctrl.currency).then(submitSuccessful, submitFailed);
		};
		
		ctrl.createContinueCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.currency.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createContinueCancel(ctrl.currency).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedSubCommpanyList = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.currency.list',
					'state' : 'cda.currency.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				ctrl.currency = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.currency.list',
					'state' : 'cda.currency.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}		
	}
})(angular);